package com.example.demo.mapper;

import com.example.demo.model.entity.achievement.HorizontalProject;
import com.example.demo.model.enums.ProjectStatusEnum;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface HorizontalProjectMapper {

    @Insert("INSERT INTO horizontal_project (" +
            "project_name, contract_number, total_amount, client_organization, " +
            "principal_investigator, executive_leader, start_date, end_date, " +
            "payment_records, participants, financial_account, payment_voucher, " +
            "completion_time, completion_certificate, final_report, " +
            "create_time, update_time, status, submit_user_id" +
            ") VALUES (" +
            "#{projectName}, #{contractNumber}, #{totalAmount}, #{clientOrganization}, " +
            "#{principalInvestigator}, #{executiveLeader}, #{startDate}, #{endDate}, " +
            "#{paymentRecords}, #{participants}, #{financialAccount}, #{paymentVoucher}, " +
            "#{completionTime}, #{completionCertificate}, #{finalReport}, " +
            "#{createTime}, #{updateTime}, #{status}, #{submitUserId}" +
            ")")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(HorizontalProject project);

    @Update("UPDATE horizontal_project SET " +
            "project_name = #{projectName}, " +
            "contract_number = #{contractNumber}, " +
            "total_amount = #{totalAmount}, " +
            "client_organization = #{clientOrganization}, " +
            "principal_investigator = #{principalInvestigator}, " +
            "executive_leader = #{executiveLeader}, " +
            "start_date = #{startDate}, " +
            "end_date = #{endDate}, " +
            "payment_records = #{paymentRecords}, " +
            "participants = #{participants}, " +
            "financial_account = #{financialAccount}, " +
            "payment_voucher = #{paymentVoucher}, " +
            "completion_time = #{completionTime}, " +
            "completion_certificate = #{completionCertificate}, " +
            "final_report = #{finalReport}, " +
            "update_time = #{updateTime}, " +
            "status = #{status} " +
            "WHERE id = #{id}")
    int update(HorizontalProject project);

    @Delete("DELETE FROM horizontal_project WHERE id = #{id}")
    int deleteById(@Param("id") Long id);

    @Select("SELECT * FROM horizontal_project WHERE id = #{id}")
    @Results({
            @Result(property = "completionCertificate", column = "completion_certificate"),
            @Result(property = "finalReport", column = "final_report"),
            @Result(property = "paymentVoucher", column = "payment_voucher"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time")
    })
    HorizontalProject selectById(@Param("id") Long id);

    @Select("SELECT * FROM horizontal_project")
    @Results({
            @Result(property = "completionCertificate", column = "completion_certificate"),
            @Result(property = "finalReport", column = "final_report"),
            @Result(property = "paymentVoucher", column = "payment_voucher"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time")
    })
    List<HorizontalProject> selectAll();

    @Select("SELECT * FROM horizontal_project WHERE submit_user_id = #{userId}")
    @Results({
            @Result(property = "completionCertificate", column = "completion_certificate"),
            @Result(property = "finalReport", column = "final_report"),
            @Result(property = "paymentVoucher", column = "payment_voucher"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time")
    })
    List<HorizontalProject> selectBySubmitUserId(@Param("userId") Long userId);

    @Select("SELECT * FROM horizontal_project WHERE status = #{status}")
    @Results({
            @Result(property = "completionCertificate", column = "completion_certificate"),
            @Result(property = "finalReport", column = "final_report"),
            @Result(property = "paymentVoucher", column = "payment_voucher"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time")
    })
    List<HorizontalProject> selectByStatus(@Param("status") ProjectStatusEnum status);

    @Select("SELECT * FROM horizontal_project WHERE project_name LIKE CONCAT('%', #{name}, '%')")
    @Results({
            @Result(property = "completionCertificate", column = "completion_certificate"),
            @Result(property = "finalReport", column = "final_report"),
            @Result(property = "paymentVoucher", column = "payment_voucher"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time")
    })
    List<HorizontalProject> selectByProjectName(@Param("name") String name);

    @Update("UPDATE horizontal_project SET status = #{status}, update_time = #{updateTime} WHERE id = #{id}")
    int updateStatus(@Param("id") Long id, @Param("status") ProjectStatusEnum status, @Param("updateTime") LocalDateTime updateTime);

    @Select("SELECT COUNT(*) FROM horizontal_project WHERE status = #{status}")
    int countByStatus(@Param("status") ProjectStatusEnum status);
}